package com.cmy.dao.user;

import com.cmy.dao.BaseDao;
import com.cmy.pojo.Role;
import com.cmy.pojo.User;
import com.mysql.jdbc.StringUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImp1 implements UserDao{
    //从数据库返回查到的user
    public User getLoginUser(Connection connection, String userCode,String password) {

        PreparedStatement pst = null;
        ResultSet rs = null;
        User user = null;
        if (connection!=null){
            String sql = "select * from smbms_user where userCode=? and userPassword=?";
            Object[] params = {userCode,password};
            try {
                rs = BaseDao.execute(connection, pst, rs, sql, params);
                //将该用户的所有信息取出来
                if (rs.next()){
                    user = new User();
                    user.setId(rs.getInt("id"));
                    user.setUserCode(rs.getString("userCode"));
                    user.setUserName(rs.getString("userName"));
                    user.setUserPassword(rs.getString("userPassword"));
                    user.setGender(rs.getInt("gender"));
                    user.setBirthday(rs.getDate("birthday"));
                    user.setPhone(rs.getString("phone"));
                    user.setAddress(rs.getString("address"));
                    user.setUserRole(rs.getInt("userRole"));
                    user.setCreatedBy(rs.getInt("createdBy"));
                    user.setModifyBy(rs.getInt("modifyBy"));
                    user.setModifyDate(rs.getDate("modifyDate"));
                }
                BaseDao.release(connection,pst,rs);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        }
        return user;
    }
    //修改当前用户的密码
    public int updatePwd(Connection connection, int id, String password) throws SQLException {
        PreparedStatement pstm = null;
        int execute = 0;
        if (connection!=null){

            Object[] params = {password,id};
            String sql = "update smbms_user set userPassword = ? where id =?";
            execute = BaseDao.execute(connection, pstm, sql, params);
            BaseDao.release(null,pstm,null);
        }
        return execute;
    }
    //从数据库获取用户数量
    public int getCount(Connection connection,String userName, int userRole) throws SQLException {
        PreparedStatement pstm = null;
        StringBuffer sql = new StringBuffer();
        ResultSet rs = null;
        int count = 0;
        if (connection!=null){
            //存放参数
            ArrayList<Object> list = new ArrayList<Object>();
            sql.append("select count(1) as count from `smbms_user` as `u`,`smbms_role` as `r` where u.userRole = r.id");

            if (!StringUtils.isNullOrEmpty(userName)){
                sql.append(" and u.username like ?");
                list.add("%"+userName+"%");
            }
            if (userRole>0){
                sql.append(" and u.userRole = ?");
                list.add(userRole);
            }


            Object[] params = list.toArray();

//            System.out.println("UserDaoImp1->getCount:"+sql.toString());
            rs = BaseDao.execute(connection, pstm, rs, sql.toString(), params);

            if (rs.next()){
                count = rs.getInt("count");
            }
            BaseDao.release(null,pstm,rs);
        }
        return count;
    }

    //根据分页信息和用户名以及角色,返回指定页面大小的用户列表
    public List<User> getUserList(Connection connection, String userName, int userRole, int currentPageNo, int pageSize) throws SQLException {
        PreparedStatement pstm = null;
        StringBuffer sql = new StringBuffer();
        ResultSet rs = null;
        List<User> userlist = new ArrayList<User>();
        int count = 0;
        if (connection!=null) {
            ArrayList<Object> list = new ArrayList<Object>();
            sql.append("select u.*,r.roleName as userRoleName from `smbms_user` as `u`,`smbms_role` as `r` " +
                       "where u.userRole = r.id");
            if (!StringUtils.isNullOrEmpty(userName)) {
                sql.append(" and u.username like ?");
                list.add("%" + userName + "%");
            }
            if (userRole > 0) {
                sql.append(" and u.userRole = ?");
                list.add(userRole);
            }

            //分页操作
            /*limit startInx,pageSize
            limit 0,5 第一页 0,1,2,3,4
            limit 5,5 第二页 5,6,7,8,9
            startInx = (当前页-1)*pagesize
            0 = (1-1)*5
            5 = (2-1)*5
            * */
            sql.append(" order by creationDate DESC limit ?,?");
//            System.out.println("sql-->:"+sql.toString());
            int startIndex = (currentPageNo - 1) * pageSize;
            list.add(startIndex);
            list.add(pageSize);
            Object[] params = list.toArray();
            rs = BaseDao.execute(connection,pstm,rs,sql.toString(),params);
            while(rs.next()){
                User _user = new User();
                _user.setId(rs.getInt("id"));
                _user.setUserCode(rs.getString("userCode"));
                _user.setUserName(rs.getString("userName"));
                _user.setGender(rs.getInt("gender"));
                _user.setBirthday(rs.getDate("birthday"));
                _user.setPhone(rs.getString("phone"));
                _user.setUserRole(rs.getInt("userRole"));
                _user.setUserRoleName(rs.getString("userRoleName"));
                _user.setModifyDate(rs.getDate("modifyDate"));
                userlist.add(_user);
            }
            BaseDao.release(null,pstm,rs);
        }
        return userlist;
    }

    //添加用户
    public boolean addUser(Connection connection, User user) throws SQLException {
        PreparedStatement pstm = null;
        boolean flag = false;
        if (connection!=null){
            String sql= "insert into `smbms_user`" +
                    "   (userCode, userName,userPassword, gender, birthday, phone, address, userRole)" +
                    "    VALUES(?,?,?,?,?,?,?,?)";
            Object[] params = {user.getUserCode(),user.getUserName(),user.getUserPassword(),user.getGender(),
                               user.getBirthday(),user.getPhone(),user.getAddress(),user.getUserRole()};
            if (user!=null){
                BaseDao.execute(connection,pstm,sql,params);
                flag = true;
            }else {
                flag =false;
            }
            BaseDao.release(null,pstm,null);
        }
        return flag;
    }

    //判断用户是否存在,存在访问true
    public boolean userCodeExist(Connection connection, String userCode) throws SQLException {
        boolean flag = false;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        if (connection!=null){
            String sql = "select *" +
                    " from smbms_user" +
                    " where userCode = ?";
            Object[] params = {userCode};
            rs = BaseDao.execute(connection, pstm, rs, sql, params);
            if (rs.next()){
                flag = true;
            }
            BaseDao.release(null,pstm,rs);
        }
        return flag;
    }
}

    